package gui;

import dao.HistoriaClinicaDAO;
import dao.TurnoDAO;
import dto.EstudioDTO;
import dto.MedicoDTO;
import dto.TurnoDTO;
import dto.UserDTO;
import java.sql.SQLException;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Collections;
import javax.swing.DefaultListModel;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
import logic.Estudio;
import logic.HistoriaClinica;
import logic.Medico;
import logic.ObraSocial;
import logic.ObraSocialPlan;
import logic.Paciente;
import logic.Turno;
import logic.User;

/**
 *
 * @author lucifer
 */
public class HistoriaClinicaPanel extends javax.swing.JPanel implements Slave {

  // Se puede preguntar quién es el médico que está editando la historia clínica
  private void test() throws SQLException {
    // cuál es user id del médico:
    int uid = ((App)master).UID();
    // y con el user id se puede buscar al usuario:
    UserDTO u = User.SearchByID(uid);
    // y con el user dto se puede crear un médico dto
    MedicoDTO m1 = Medico.SearchByID(u.getIdMedico());
    // ó se puede hacer todo junto:
    MedicoDTO m2 = Medico.SearchByID(User.SearchByID(((App)master).UID()).getIdMedico());
  }
  
  private boolean isMe(int id_medico){
    boolean b = false;
    try {
      int uid = ((App)master).UID();
      UserDTO u = User.SearchByID(uid);
      if(u.getIdMedico() == id_medico){
        b = true;
      }  
    } catch (Exception e) {
    }
    return b;
  }

  private Master master;

  public void SetMaster(Master master) {
    this.master = master;
  }

  public void SetModeActions() {
    //nil
  }

  private ArrayList<Integer> cIDs;

  private Paciente pacienteSeleccionado;
  private HistoriaClinica historiaClinica;
  private boolean isInHistoria;
  private ArrayList<TurnoDTO> tdtos;
  private ArrayList<Estudio> estudios;
  private Estudio estudioAUX;
  private boolean isEnFecha;
  private boolean isEstudioNuevo;
  private String diagBack;
  
  public void SetPaciente(Paciente p){
    this.pacienteSeleccionado = p;
  }
  public boolean getIsInHistoria(){
    return isInHistoria;
  }
  public void setIsInHistoria(boolean b){
    this.isInHistoria = b;
  }
  public HistoriaClinicaPanel() {
    SetMaster(master);
    initComponents();
  }

  public HistoriaClinicaPanel(Master master) {
    SetMaster(master);
    initComponents();
    setBotonesOff();
    vaciarTablaConsultas();
    isEnFecha = false;
  }
  
  public void setCamposEditables(boolean b){
    patologiasjTextField.setEditable(b);
    alergiasjTextField.setEditable(b);
  }
  
  private void llenarTdtos(){
    try {
      TurnoDAO tdao = TurnoDAO.createDAO();
      tdtos = tdao.SearchByPacienteID(historiaClinica.getIdPaciente(), Turno.asistido);
      Collections.reverse(tdtos);
    } catch (Exception e) {
    }
  }
  
  private void llenarEstudios(int id_turno) throws SQLException{
    HistoriaClinicaDAO hdao = HistoriaClinicaDAO.createDAO();
    ArrayList<EstudioDTO> edto = hdao.getEstudios(id_turno);
    estudios = new ArrayList();
    for (EstudioDTO e : edto){
      estudios.add(new Estudio(e));
    }
  }
  
  private void setBotonesOff(){
    eliminarPatologiajButton.setEnabled(false);
    eliminarAlergiajButton.setEnabled(false);
    agregarPatologiajButton.setEnabled(false);
    agregarAlergiajButton.setEnabled(false);
  }
  private void vaciarTablaConsultas(){
    DefaultTableModel m = new DefaultTableModel();
    m.addColumn("Fecha de atención");
    m.addColumn("Atendido por");
    m.addColumn("Estudios");
    m.addColumn("Diagnóstico");
    consultasjTable.setColumnSelectionAllowed(false);
    consultasjTable.setModel(m);
    consultasjTable.getColumnModel().getColumn(2).setPreferredWidth(20);
    turnoSelecjLabel.setText("-");
  }
  
  private void vaciarTablaEstudios(){
    DefaultListModel m2 = new DefaultListModel();
    estudiosjList.setModel(m2);
    detallejTextArea.setText("");
  }
  
  private void actualizarTablaEstudios(){
    DefaultListModel m = new DefaultListModel();
    if (estudios.size() == 0){
      vaciarTablaEstudios();
    }
    else{
      for (Estudio e : estudios) {
            m.addElement(e);         
      }
      estudiosjList.setModel(m);
    }
  }
  private String nombEst(){
    String s="";
    for (Estudio e: estudios){
      s=s+", "+e.getNombre();
    }
    return s;
  }
  
  private void updateTablaConsultas(){
    DefaultTableModel m = (DefaultTableModel) consultasjTable.getModel();
    for (TurnoDTO t : tdtos) {
      MedicoDTO mdto;
      try {
          llenarEstudios(t.getId());
          mdto = Medico.SearchByID(t.getIdMedico());
          String fecha = t.getFecha().toLocalDate().format(DateTimeFormatter.ofPattern("dd/MM/yyyy"));
          HistoriaClinicaDAO hdao = HistoriaClinicaDAO.createDAO();
          String diag = hdao.getDiagnostico(t.getId());
          m.addRow(
          new Object[] {
          fecha,
          mdto.getNombre()+" "+mdto.getApellido(),
          "("+estudios.size()+")"+nombEst(),
          diag
        }
      );
      } catch (Exception e) {
      }
    }
    estudios = null;
  }
  
  private void refreshTablaConsultas(){
    DefaultTableModel m = (DefaultTableModel) consultasjTable.getModel();
    m.setValueAt("("+estudios.size()+")"+nombEst(), consultasjTable.getSelectedRow(), 2);
    m.setValueAt(diagnosticojTextArea.getText(), consultasjTable.getSelectedRow(), 3);
  }
  
  public void UpdateDatosPaciente() throws SQLException {
    if (pacienteSeleccionado != null){
      pacientejLabel1.setText(pacienteSeleccionado.getNombre()+" "+pacienteSeleccionado.getApellido());
      dnijLabel1.setText(String.valueOf(pacienteSeleccionado.getDni()));
      nafiliadojLabel1.setText(String.valueOf(pacienteSeleccionado.getNAfiliado()));
      try {
          HistoriaClinicaDAO hdao = HistoriaClinicaDAO.createDAO();
          if(hdao.HistoriaClinicaDTO(pacienteSeleccionado.getId()) == null){
            hdao.CrearHistoria(pacienteSeleccionado.getId());
          }
          historiaClinica = new HistoriaClinica(hdao.HistoriaClinicaDTO(pacienteSeleccionado.getId()));
          obraSocialjLabel1.setText(ObraSocial.SearchByID(pacienteSeleccionado.getIdObraSocial()).getNombre());
          planjLabel1.setText(ObraSocialPlan.SearchByID(pacienteSeleccionado.getIdPlanObraSocial()).getNombre());
          DefaultListModel listModel= new DefaultListModel();
          ArrayList<String> lista;
          lista=hdao.getAlergias(historiaClinica.getId());
          for (String s : lista){
            listModel.addElement(s);
          }
          alergiasjList.setModel(listModel);
          lista=hdao.getPatologias(historiaClinica.getId());
          listModel= new DefaultListModel();
          for (String s : lista){
            listModel.addElement(s);
          }
          patologiasjList.setModel(listModel);

      } catch (Exception e) {
      }
      llenarTdtos();
      updateTablaConsultas();
      vaciarTablaEstudios();
    } else{
        historiaClinica = null;
        pacientejLabel1.setText("Paciente no seleccionado");
        dnijLabel1.setText("-");
        nafiliadojLabel1.setText("-");
        obraSocialjLabel1.setText("-");
        planjLabel1.setText("-");
        DefaultListModel listModel= new DefaultListModel();
        alergiasjList.setModel(listModel);
        patologiasjList.setModel(listModel);   
    }
    setBotonesOff();
    patologiasjTextField.setText("");
    alergiasjTextField.setText("");
  }
  
  private void setBotonesInicio(){
    nuevoEstudiojButton.setEnabled(true);
    modificarEstudiojButton.setEnabled(false);
    aceptarEstudiojButton.setEnabled(false);
    cancelarEstudiojButton.setEnabled(false);
    eliminarEstudiojButton.setEnabled(false);
    estudiosjTextField.setEditable(false);
    detallejTextArea.setEditable(false);
  }
  
  private void setBotonesAyC(boolean b){
    aceptarEstudiojButton.setEnabled(b);
    cancelarEstudiojButton.setEnabled(b);
    estudiosjTextField.setEditable(b);
    detallejTextArea.setEditable(b);
  }
  
  private void setBotonesNyM(boolean b){
    nuevoEstudiojButton.setEnabled(b);
    modificarEstudiojButton.setEnabled(b);
    estudiosjTextField.setEditable(!b);
    detallejTextArea.setEditable(!b);
  }
  
  private void setTextosEditable(boolean b){
    estudiosjTextField.setEditable(b);
    detallejTextArea.setEditable(b);
  }
  
  private void limpiarTextos(){
    detallejTextArea.setText("");
    estudiosjTextField.setText("");
  }
  
  private void botonesEstudioOff(){
    nuevoEstudiojButton.setEnabled(false);
    modificarEstudiojButton.setEnabled(false);
    aceptarEstudiojButton.setEnabled(false);
    cancelarEstudiojButton.setEnabled(false);
    eliminarEstudiojButton.setEnabled(false);
  }
  
  private void botonesEstudioInicial(){
    nuevoEstudiojButton.setEnabled(true);
    modificarEstudiojButton.setEnabled(false);
    aceptarEstudiojButton.setEnabled(false);
    cancelarEstudiojButton.setEnabled(false);
    eliminarEstudiojButton.setEnabled(false);
  }

  /**
   * This method is called from within the constructor to initialize the form.
   * WARNING: Do NOT modify this code. The content of this method is always
   * regenerated by the Form Editor.
   */
  @SuppressWarnings("unchecked")
  // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
  private void initComponents() {

    seleccionarPacientejButton = new javax.swing.JButton();
    pacientejLabel1 = new javax.swing.JLabel();
    pacientejLabel = new javax.swing.JLabel();
    dnijLabel = new javax.swing.JLabel();
    dnijLabel1 = new javax.swing.JLabel();
    obraSocialjLabel = new javax.swing.JLabel();
    obraSocialjLabel1 = new javax.swing.JLabel();
    planjLabel = new javax.swing.JLabel();
    planjLabel1 = new javax.swing.JLabel();
    nafiliadojLabel = new javax.swing.JLabel();
    nafiliadojLabel1 = new javax.swing.JLabel();
    alergiasjPanel = new javax.swing.JPanel();
    agregarAlergiajButton = new javax.swing.JButton();
    eliminarAlergiajButton = new javax.swing.JButton();
    alergiasjTextField = new javax.swing.JTextField();
    alergiasjScrollPane = new javax.swing.JScrollPane();
    alergiasjList = new javax.swing.JList();
    patologiasjPanel = new javax.swing.JPanel();
    agregarPatologiajButton = new javax.swing.JButton();
    eliminarPatologiajButton = new javax.swing.JButton();
    patologiasjTextField = new javax.swing.JTextField();
    patologiasjScrollPane = new javax.swing.JScrollPane();
    patologiasjList = new javax.swing.JList();
    jTabbedPane1 = new javax.swing.JTabbedPane();
    consultasjScrollPane = new javax.swing.JScrollPane();
    consultasjTable = new javax.swing.JTable();
    hidejPanel = new javax.swing.JPanel();
    resultadosEstudiosjPanel = new javax.swing.JPanel();
    detallejScrollPane = new javax.swing.JScrollPane();
    detallejTextArea = new javax.swing.JTextArea();
    aceptarEstudiojButton = new javax.swing.JButton();
    cancelarEstudiojButton = new javax.swing.JButton();
    eliminarEstudiojButton = new javax.swing.JButton();
    estudiosjTextField = new javax.swing.JTextField();
    jScrollPane1 = new javax.swing.JScrollPane();
    estudiosjList = new javax.swing.JList();
    modificarEstudiojButton = new javax.swing.JButton();
    nuevoEstudiojButton = new javax.swing.JButton();
    diagnosticojPanel = new javax.swing.JPanel();
    jScrollPane8 = new javax.swing.JScrollPane();
    diagnosticojTextArea = new javax.swing.JTextArea();
    guardarDiagnosticojButton = new javax.swing.JButton();
    editarDiagnosticojButton = new javax.swing.JButton();
    jLabel1 = new javax.swing.JLabel();
    turnoSelecjLabel = new javax.swing.JLabel();

    seleccionarPacientejButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/gui/pacientes.png"))); // NOI18N
    seleccionarPacientejButton.setText("Seleccionar paciente");
    seleccionarPacientejButton.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        seleccionarPacientejButtonActionPerformed(evt);
      }
    });

    pacientejLabel1.setFont(new java.awt.Font("DejaVu Sans", 1, 12)); // NOI18N
    pacientejLabel1.setText("Paciente no seleccionado");

    pacientejLabel.setText("Paciente:");

    dnijLabel.setText("DNI:");

    dnijLabel1.setText("-");

    obraSocialjLabel.setText("Obra social:");

    obraSocialjLabel1.setText("-");

    planjLabel.setText("Plan:");

    planjLabel1.setText("-");

    nafiliadojLabel.setText("N° de Afiliado:");

    nafiliadojLabel1.setText("-");

    alergiasjPanel.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Alergias", javax.swing.border.TitledBorder.LEFT, javax.swing.border.TitledBorder.TOP));

    agregarAlergiajButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/gui/add.png"))); // NOI18N
    agregarAlergiajButton.setText("Agregar");
    agregarAlergiajButton.setMaximumSize(new java.awt.Dimension(128, 17));
    agregarAlergiajButton.setMinimumSize(new java.awt.Dimension(128, 27));
    agregarAlergiajButton.setPreferredSize(new java.awt.Dimension(128, 27));
    agregarAlergiajButton.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        agregarAlergiajButtonActionPerformed(evt);
      }
    });

    eliminarAlergiajButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/gui/remove.png"))); // NOI18N
    eliminarAlergiajButton.setText("Eliminar");
    eliminarAlergiajButton.setMaximumSize(new java.awt.Dimension(128, 27));
    eliminarAlergiajButton.setMinimumSize(new java.awt.Dimension(128, 27));
    eliminarAlergiajButton.setPreferredSize(new java.awt.Dimension(128, 27));
    eliminarAlergiajButton.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        eliminarAlergiajButtonActionPerformed(evt);
      }
    });

    alergiasjTextField.setEditable(false);
    alergiasjTextField.setMaximumSize(new java.awt.Dimension(262, 27));
    alergiasjTextField.setMinimumSize(new java.awt.Dimension(262, 27));
    alergiasjTextField.setPreferredSize(new java.awt.Dimension(262, 27));
    alergiasjTextField.addKeyListener(new java.awt.event.KeyAdapter() {
      public void keyTyped(java.awt.event.KeyEvent evt) {
        alergiasjTextFieldKeyTyped(evt);
      }
    });

    alergiasjList.setModel(new DefaultListModel());
    alergiasjList.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
    alergiasjList.addFocusListener(new java.awt.event.FocusAdapter() {
      public void focusGained(java.awt.event.FocusEvent evt) {
        alergiasjListFocusGained(evt);
      }
    });
    alergiasjScrollPane.setViewportView(alergiasjList);

    javax.swing.GroupLayout alergiasjPanelLayout = new javax.swing.GroupLayout(alergiasjPanel);
    alergiasjPanel.setLayout(alergiasjPanelLayout);
    alergiasjPanelLayout.setHorizontalGroup(
      alergiasjPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addComponent(alergiasjScrollPane)
      .addComponent(alergiasjTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
      .addGroup(alergiasjPanelLayout.createSequentialGroup()
        .addComponent(agregarAlergiajButton, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
        .addComponent(eliminarAlergiajButton, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
    );
    alergiasjPanelLayout.setVerticalGroup(
      alergiasjPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addGroup(alergiasjPanelLayout.createSequentialGroup()
        .addContainerGap()
        .addComponent(alergiasjScrollPane, javax.swing.GroupLayout.DEFAULT_SIZE, 123, Short.MAX_VALUE)
        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
        .addComponent(alergiasjTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
        .addGroup(alergiasjPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
          .addComponent(agregarAlergiajButton, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
          .addComponent(eliminarAlergiajButton, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
    );

    patologiasjPanel.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Patologías", javax.swing.border.TitledBorder.LEFT, javax.swing.border.TitledBorder.TOP));

    agregarPatologiajButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/gui/add.png"))); // NOI18N
    agregarPatologiajButton.setText("Agregar");
    agregarPatologiajButton.setMaximumSize(new java.awt.Dimension(128, 27));
    agregarPatologiajButton.setMinimumSize(new java.awt.Dimension(128, 27));
    agregarPatologiajButton.setPreferredSize(new java.awt.Dimension(128, 27));
    agregarPatologiajButton.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        agregarPatologiajButtonActionPerformed(evt);
      }
    });

    eliminarPatologiajButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/gui/remove.png"))); // NOI18N
    eliminarPatologiajButton.setText("Eliminar");
    eliminarPatologiajButton.setMaximumSize(new java.awt.Dimension(128, 27));
    eliminarPatologiajButton.setMinimumSize(new java.awt.Dimension(128, 27));
    eliminarPatologiajButton.setPreferredSize(new java.awt.Dimension(128, 27));
    eliminarPatologiajButton.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        eliminarPatologiajButtonActionPerformed(evt);
      }
    });

    patologiasjTextField.setEditable(false);
    patologiasjTextField.setMaximumSize(new java.awt.Dimension(262, 27));
    patologiasjTextField.setMinimumSize(new java.awt.Dimension(262, 27));
    patologiasjTextField.setPreferredSize(new java.awt.Dimension(262, 27));
    patologiasjTextField.addKeyListener(new java.awt.event.KeyAdapter() {
      public void keyTyped(java.awt.event.KeyEvent evt) {
        patologiasjTextFieldKeyTyped(evt);
      }
    });

    patologiasjList.setModel(new DefaultListModel()
    );
    patologiasjList.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
    patologiasjList.addFocusListener(new java.awt.event.FocusAdapter() {
      public void focusGained(java.awt.event.FocusEvent evt) {
        patologiasjListFocusGained(evt);
      }
    });
    patologiasjScrollPane.setViewportView(patologiasjList);

    javax.swing.GroupLayout patologiasjPanelLayout = new javax.swing.GroupLayout(patologiasjPanel);
    patologiasjPanel.setLayout(patologiasjPanelLayout);
    patologiasjPanelLayout.setHorizontalGroup(
      patologiasjPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addComponent(patologiasjScrollPane)
      .addGroup(patologiasjPanelLayout.createSequentialGroup()
        .addGroup(patologiasjPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
          .addComponent(patologiasjTextField, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
          .addGroup(patologiasjPanelLayout.createSequentialGroup()
            .addComponent(agregarPatologiajButton, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
            .addComponent(eliminarPatologiajButton, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
        .addGap(0, 0, Short.MAX_VALUE))
    );
    patologiasjPanelLayout.setVerticalGroup(
      patologiasjPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addGroup(patologiasjPanelLayout.createSequentialGroup()
        .addContainerGap()
        .addComponent(patologiasjScrollPane, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)
        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
        .addComponent(patologiasjTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
        .addGroup(patologiasjPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
          .addComponent(agregarPatologiajButton, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
          .addComponent(eliminarPatologiajButton, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        .addGap(0, 0, 0))
    );

    consultasjTable.setModel(new javax.swing.table.DefaultTableModel(
      new Object [][] {
        {"Hoy", "Ramón Carrillo", "No"},
        {"16/03/14", "Leonard McCoy", "(3) Audiometría, Glóbulos rojos, Orina"},
        {"05/08/13", "Ernesto Guevara", "(1) Glóbulos blancos"},
        {"02/05/13", "René Favaloro", "(2) Electrocardiograma, Ergometría"},
        {"29/11/12", "Ernesto Guevara", "No"}
      },
      new String [] {
        "Fecha de atención", "Atendido por", "Estudios"
      }
    ) {
      Class[] types = new Class [] {
        java.lang.String.class, java.lang.String.class, java.lang.String.class
      };
      boolean[] canEdit = new boolean [] {
        false, false, false
      };

      public Class getColumnClass(int columnIndex) {
        return types [columnIndex];
      }

      public boolean isCellEditable(int rowIndex, int columnIndex) {
        return canEdit [columnIndex];
      }
    });
    consultasjTable.getTableHeader().setReorderingAllowed(false);
    consultasjTable.addMouseListener(new java.awt.event.MouseAdapter() {
      public void mouseClicked(java.awt.event.MouseEvent evt) {
        consultasjTableMouseClicked(evt);
      }
    });
    consultasjScrollPane.setViewportView(consultasjTable);
    consultasjTable.getColumnModel().getSelectionModel().setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
    if (consultasjTable.getColumnModel().getColumnCount() > 0) {
      consultasjTable.getColumnModel().getColumn(0).setResizable(false);
      consultasjTable.getColumnModel().getColumn(1).setResizable(false);
      consultasjTable.getColumnModel().getColumn(2).setResizable(false);
    }

    jTabbedPane1.addTab("Consultas", consultasjScrollPane);

    resultadosEstudiosjPanel.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Resultados de Estudios", javax.swing.border.TitledBorder.LEFT, javax.swing.border.TitledBorder.TOP));

    detallejScrollPane.setHorizontalScrollBarPolicy(javax.swing.ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
    detallejScrollPane.setVerticalScrollBarPolicy(javax.swing.ScrollPaneConstants.VERTICAL_SCROLLBAR_NEVER);

    detallejTextArea.setEditable(false);
    detallejTextArea.setColumns(20);
    detallejTextArea.setRows(5);
    detallejScrollPane.setViewportView(detallejTextArea);
    detallejTextArea.setLineWrap(true);
    detallejTextArea.setWrapStyleWord(true);

    aceptarEstudiojButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/gui/aceptar.png"))); // NOI18N
    aceptarEstudiojButton.setText("Aceptar");
    aceptarEstudiojButton.setEnabled(false);
    aceptarEstudiojButton.setMaximumSize(new java.awt.Dimension(156, 27));
    aceptarEstudiojButton.setMinimumSize(new java.awt.Dimension(156, 27));
    aceptarEstudiojButton.setPreferredSize(new java.awt.Dimension(156, 27));
    aceptarEstudiojButton.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        aceptarEstudiojButtonActionPerformed(evt);
      }
    });

    cancelarEstudiojButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/gui/cancelar.png"))); // NOI18N
    cancelarEstudiojButton.setText("Cancelar");
    cancelarEstudiojButton.setEnabled(false);
    cancelarEstudiojButton.setMaximumSize(new java.awt.Dimension(156, 27));
    cancelarEstudiojButton.setMinimumSize(new java.awt.Dimension(156, 27));
    cancelarEstudiojButton.setPreferredSize(new java.awt.Dimension(156, 27));
    cancelarEstudiojButton.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        cancelarEstudiojButtonActionPerformed(evt);
      }
    });

    eliminarEstudiojButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/gui/remove.png"))); // NOI18N
    eliminarEstudiojButton.setToolTipText("Eliminar");
    eliminarEstudiojButton.setEnabled(false);
    eliminarEstudiojButton.setMaximumSize(new java.awt.Dimension(44, 27));
    eliminarEstudiojButton.setMinimumSize(new java.awt.Dimension(44, 27));
    eliminarEstudiojButton.setPreferredSize(new java.awt.Dimension(44, 27));
    eliminarEstudiojButton.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        eliminarEstudiojButtonActionPerformed(evt);
      }
    });

    estudiosjTextField.setEditable(false);
    estudiosjTextField.setMaximumSize(new java.awt.Dimension(209, 27));
    estudiosjTextField.setMinimumSize(new java.awt.Dimension(209, 27));
    estudiosjTextField.setPreferredSize(new java.awt.Dimension(209, 27));

    estudiosjList.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
    estudiosjList.addMouseListener(new java.awt.event.MouseAdapter() {
      public void mouseClicked(java.awt.event.MouseEvent evt) {
        estudiosjListMouseClicked(evt);
      }
    });
    jScrollPane1.setViewportView(estudiosjList);

    modificarEstudiojButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/gui/modificar.png"))); // NOI18N
    modificarEstudiojButton.setToolTipText("Modificar");
    modificarEstudiojButton.setEnabled(false);
    modificarEstudiojButton.setMaximumSize(new java.awt.Dimension(44, 27));
    modificarEstudiojButton.setMinimumSize(new java.awt.Dimension(44, 27));
    modificarEstudiojButton.setPreferredSize(new java.awt.Dimension(44, 27));
    modificarEstudiojButton.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        modificarEstudiojButtonActionPerformed(evt);
      }
    });

    nuevoEstudiojButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/gui/add.png"))); // NOI18N
    nuevoEstudiojButton.setToolTipText("Agregar");
    nuevoEstudiojButton.setEnabled(false);
    nuevoEstudiojButton.setMaximumSize(new java.awt.Dimension(44, 27));
    nuevoEstudiojButton.setMinimumSize(new java.awt.Dimension(44, 27));
    nuevoEstudiojButton.setPreferredSize(new java.awt.Dimension(44, 27));
    nuevoEstudiojButton.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        nuevoEstudiojButtonActionPerformed(evt);
      }
    });

    javax.swing.GroupLayout resultadosEstudiosjPanelLayout = new javax.swing.GroupLayout(resultadosEstudiosjPanel);
    resultadosEstudiosjPanel.setLayout(resultadosEstudiosjPanelLayout);
    resultadosEstudiosjPanelLayout.setHorizontalGroup(
      resultadosEstudiosjPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addGroup(resultadosEstudiosjPanelLayout.createSequentialGroup()
        .addGroup(resultadosEstudiosjPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
          .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)
          .addComponent(estudiosjTextField, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
        .addComponent(detallejScrollPane))
      .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, resultadosEstudiosjPanelLayout.createSequentialGroup()
        .addComponent(nuevoEstudiojButton, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
        .addComponent(eliminarEstudiojButton, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
        .addComponent(modificarEstudiojButton, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
        .addComponent(aceptarEstudiojButton, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
        .addComponent(cancelarEstudiojButton, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
    );
    resultadosEstudiosjPanelLayout.setVerticalGroup(
      resultadosEstudiosjPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addGroup(resultadosEstudiosjPanelLayout.createSequentialGroup()
        .addGap(6, 6, 6)
        .addGroup(resultadosEstudiosjPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
          .addGroup(resultadosEstudiosjPanelLayout.createSequentialGroup()
            .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 124, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
            .addComponent(estudiosjTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
          .addComponent(detallejScrollPane, javax.swing.GroupLayout.PREFERRED_SIZE, 157, javax.swing.GroupLayout.PREFERRED_SIZE))
        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
        .addGroup(resultadosEstudiosjPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
          .addGroup(resultadosEstudiosjPanelLayout.createSequentialGroup()
            .addGap(0, 0, Short.MAX_VALUE)
            .addComponent(eliminarEstudiojButton, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
          .addComponent(modificarEstudiojButton, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
          .addGroup(resultadosEstudiosjPanelLayout.createSequentialGroup()
            .addGroup(resultadosEstudiosjPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
              .addComponent(nuevoEstudiojButton, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
              .addGroup(resultadosEstudiosjPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(aceptarEstudiojButton, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addComponent(cancelarEstudiojButton, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
            .addGap(0, 0, Short.MAX_VALUE))))
    );

    diagnosticojPanel.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Diagnóstico", javax.swing.border.TitledBorder.LEFT, javax.swing.border.TitledBorder.TOP));

    diagnosticojTextArea.setEditable(false);
    diagnosticojTextArea.setColumns(20);
    diagnosticojTextArea.setLineWrap(true);
    diagnosticojTextArea.setRows(5);
    diagnosticojTextArea.setWrapStyleWord(true);
    jScrollPane8.setViewportView(diagnosticojTextArea);

    guardarDiagnosticojButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/gui/save.png"))); // NOI18N
    guardarDiagnosticojButton.setText("Guardar");
    guardarDiagnosticojButton.setEnabled(false);
    guardarDiagnosticojButton.setMaximumSize(new java.awt.Dimension(156, 27));
    guardarDiagnosticojButton.setMinimumSize(new java.awt.Dimension(156, 27));
    guardarDiagnosticojButton.setPreferredSize(new java.awt.Dimension(156, 27));
    guardarDiagnosticojButton.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        guardarDiagnosticojButtonActionPerformed(evt);
      }
    });

    editarDiagnosticojButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/gui/modificar.png"))); // NOI18N
    editarDiagnosticojButton.setText("Editar");
    editarDiagnosticojButton.setEnabled(false);
    editarDiagnosticojButton.setMaximumSize(new java.awt.Dimension(156, 27));
    editarDiagnosticojButton.setMinimumSize(new java.awt.Dimension(156, 27));
    editarDiagnosticojButton.setPreferredSize(new java.awt.Dimension(156, 27));
    editarDiagnosticojButton.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        editarDiagnosticojButtonActionPerformed(evt);
      }
    });

    javax.swing.GroupLayout diagnosticojPanelLayout = new javax.swing.GroupLayout(diagnosticojPanel);
    diagnosticojPanel.setLayout(diagnosticojPanelLayout);
    diagnosticojPanelLayout.setHorizontalGroup(
      diagnosticojPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addGroup(diagnosticojPanelLayout.createSequentialGroup()
        .addGap(0, 0, Short.MAX_VALUE)
        .addComponent(guardarDiagnosticojButton, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
        .addComponent(editarDiagnosticojButton, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
      .addComponent(jScrollPane8)
    );
    diagnosticojPanelLayout.setVerticalGroup(
      diagnosticojPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addGroup(diagnosticojPanelLayout.createSequentialGroup()
        .addContainerGap()
        .addComponent(jScrollPane8, javax.swing.GroupLayout.PREFERRED_SIZE, 124, javax.swing.GroupLayout.PREFERRED_SIZE)
        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
        .addGroup(diagnosticojPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
          .addComponent(guardarDiagnosticojButton, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
          .addComponent(editarDiagnosticojButton, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
    );

    javax.swing.GroupLayout hidejPanelLayout = new javax.swing.GroupLayout(hidejPanel);
    hidejPanel.setLayout(hidejPanelLayout);
    hidejPanelLayout.setHorizontalGroup(
      hidejPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addGroup(hidejPanelLayout.createSequentialGroup()
        .addGroup(hidejPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
          .addComponent(resultadosEstudiosjPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
          .addGroup(hidejPanelLayout.createSequentialGroup()
            .addContainerGap()
            .addComponent(diagnosticojPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
    );
    hidejPanelLayout.setVerticalGroup(
      hidejPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addGroup(hidejPanelLayout.createSequentialGroup()
        .addComponent(resultadosEstudiosjPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
        .addComponent(diagnosticojPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        .addContainerGap())
    );

    jTabbedPane1.addTab("Detalle de la consulta", hidejPanel);

    jLabel1.setText("Turno seleccionado:");

    turnoSelecjLabel.setText("-");

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
    this.setLayout(layout);
    layout.setHorizontalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addComponent(seleccionarPacientejButton, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 192, javax.swing.GroupLayout.PREFERRED_SIZE)
      .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
          .addGroup(layout.createSequentialGroup()
            .addGap(15, 15, 15)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
              .addComponent(planjLabel)
              .addComponent(obraSocialjLabel)
              .addComponent(dnijLabel)
              .addComponent(pacientejLabel)
              .addComponent(nafiliadojLabel))
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
              .addComponent(pacientejLabel1)
              .addComponent(dnijLabel1)
              .addComponent(obraSocialjLabel1)
              .addComponent(planjLabel1)
              .addComponent(nafiliadojLabel1))
            .addGap(6, 6, 6))
          .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
              .addComponent(alergiasjPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
              .addComponent(patologiasjPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)))
        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
          .addComponent(jTabbedPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
          .addGroup(layout.createSequentialGroup()
            .addComponent(jLabel1)
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
            .addComponent(turnoSelecjLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 194, javax.swing.GroupLayout.PREFERRED_SIZE))))
    );
    layout.setVerticalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addGroup(layout.createSequentialGroup()
        .addGap(0, 0, 0)
        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
          .addComponent(seleccionarPacientejButton)
          .addComponent(pacientejLabel)
          .addComponent(pacientejLabel1))
        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
          .addComponent(dnijLabel)
          .addComponent(dnijLabel1))
        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
          .addComponent(obraSocialjLabel)
          .addComponent(obraSocialjLabel1))
        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
          .addComponent(planjLabel)
          .addComponent(planjLabel1))
        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
          .addComponent(nafiliadojLabel)
          .addComponent(nafiliadojLabel1)
          .addComponent(jLabel1)
          .addComponent(turnoSelecjLabel))
        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
          .addGroup(layout.createSequentialGroup()
            .addComponent(patologiasjPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
            .addComponent(alergiasjPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
          .addComponent(jTabbedPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
    );
  }// </editor-fold>//GEN-END:initComponents

  private void seleccionarPacientejButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_seleccionarPacientejButtonActionPerformed
    master.ShowSlave(master.PacienteGrepPanel());
    isInHistoria = true;
    jTabbedPane1.setSelectedIndex(0);
    vaciarTablaConsultas();
    vaciarTablaEstudios();
    botonesEstudioOff();
    limpiarTextos();
    editarDiagnosticojButton.setEnabled(false);
    guardarDiagnosticojButton.setEnabled(false);
    diagnosticojTextArea.setText("");
    diagnosticojTextArea.setEditable(false);
    detallejTextArea.setEditable(false);
    estudiosjTextField.setEditable(false);
  }//GEN-LAST:event_seleccionarPacientejButtonActionPerformed

  private void agregarPatologiajButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_agregarPatologiajButtonActionPerformed
    if (historiaClinica != null && !patologiasjTextField.getText().equals("")){
      DefaultListModel listModel = (DefaultListModel) patologiasjList.getModel();
      listModel.addElement(patologiasjTextField.getText());
      patologiasjList.setModel(listModel);
      try {
        HistoriaClinicaDAO hdao = HistoriaClinicaDAO.createDAO();
        hdao.AddPatologia(historiaClinica.getId(), patologiasjTextField.getText());
      } catch (Exception e) {

      }
      patologiasjTextField.setText("");
    }
    agregarPatologiajButton.setEnabled(false);
  }//GEN-LAST:event_agregarPatologiajButtonActionPerformed

  private void agregarAlergiajButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_agregarAlergiajButtonActionPerformed
    if (historiaClinica != null && !alergiasjTextField.getText().equals("")){
      DefaultListModel listModel = (DefaultListModel) alergiasjList.getModel();
      listModel.addElement(alergiasjTextField.getText());
      alergiasjList.setModel(listModel);
      try {
        HistoriaClinicaDAO hdao = HistoriaClinicaDAO.createDAO();
        hdao.AddAlergia(historiaClinica.getId(), alergiasjTextField.getText());
      } catch (Exception e) {

      }
      alergiasjTextField.setText("");
    }
    agregarAlergiajButton.setEnabled(false);
  }//GEN-LAST:event_agregarAlergiajButtonActionPerformed

  private void eliminarPatologiajButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_eliminarPatologiajButtonActionPerformed
      DefaultListModel listModel = (DefaultListModel) patologiasjList.getModel();
      String remov = String.valueOf(patologiasjList.getSelectedValue());
      try {
        HistoriaClinicaDAO hdao = HistoriaClinicaDAO.createDAO();
        hdao.DelPatologia(historiaClinica.getId(), remov);
      } catch (Exception e) { 
      }
      listModel.removeElementAt(patologiasjList.getSelectedIndex());
      patologiasjList.setModel(listModel);
      eliminarPatologiajButton.setEnabled(false);
  }//GEN-LAST:event_eliminarPatologiajButtonActionPerformed

  private void eliminarAlergiajButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_eliminarAlergiajButtonActionPerformed
    DefaultListModel listModel = (DefaultListModel) alergiasjList.getModel();
    String remov = String.valueOf(alergiasjList.getSelectedValue());
    try {
      HistoriaClinicaDAO hdao = HistoriaClinicaDAO.createDAO();
      hdao.DelAlergia(historiaClinica.getId(), remov);
    } catch (Exception e) { 
    }
    listModel.removeElementAt(alergiasjList.getSelectedIndex());
    alergiasjList.setModel(listModel);
    eliminarAlergiajButton.setEnabled(false);
  }//GEN-LAST:event_eliminarAlergiajButtonActionPerformed

  private void patologiasjListFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_patologiasjListFocusGained
    if (!patologiasjList.isSelectionEmpty()){
      eliminarPatologiajButton.setEnabled(true);
    }  
  }//GEN-LAST:event_patologiasjListFocusGained

  private void alergiasjListFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_alergiasjListFocusGained
    if (!alergiasjList.isSelectionEmpty()){
      eliminarAlergiajButton.setEnabled(true);
    }  
  }//GEN-LAST:event_alergiasjListFocusGained

  private void patologiasjTextFieldKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_patologiasjTextFieldKeyTyped
    if (historiaClinica != null){
     agregarPatologiajButton.setEnabled(true);
    } 
  }//GEN-LAST:event_patologiasjTextFieldKeyTyped

  private void alergiasjTextFieldKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_alergiasjTextFieldKeyTyped
    if (historiaClinica != null){
     agregarAlergiajButton.setEnabled(true);
    } 
  }//GEN-LAST:event_alergiasjTextFieldKeyTyped

  private void consultasjTableMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_consultasjTableMouseClicked
    int sel = consultasjTable.getSelectedRow();
    System.out.println(sel);
    if (sel == -1) {
      return;
    }
    try {
      TurnoDTO turno = tdtos.get(sel);
      llenarEstudios(turno.getId());
      vaciarTablaEstudios();
      actualizarTablaEstudios();
      HistoriaClinicaDAO hdao = HistoriaClinicaDAO.createDAO();
      diagnosticojTextArea.setText(diagBack = hdao.getDiagnostico(turno.getId()));
      if (!diagnosticojTextArea.getText().equals("")){
        guardarDiagnosticojButton.setEnabled(false);
        editarDiagnosticojButton.setEnabled(true);
      }else{
        guardarDiagnosticojButton.setEnabled(true);
        editarDiagnosticojButton.setEnabled(false);
      }
      setTextosEditable(false);
      limpiarTextos();
      turnoSelecjLabel.setText(turno.getFecha().format(DateTimeFormatter.ofPattern("dd/MM/yyyy HH:mm")));
      if (!turno.getFecha().toLocalDate().isEqual(LocalDate.now()) || !isMe(turno.getIdMedico())){//Permite editar en fecha del dia unicamente
        diagnosticojTextArea.setEditable(false);
        botonesEstudioOff();
        isEnFecha = false;
        guardarDiagnosticojButton.setEnabled(false);
        editarDiagnosticojButton.setEnabled(false);
      }else {                                                                 //Entra si es la fecha de hoy
        diagnosticojTextArea.setEditable(true);
        botonesEstudioInicial();
        isEnFecha = true;
      }
      jTabbedPane1.setSelectedIndex(1);
    } catch (SQLException e) {
      System.out.println("error");
    }
  }//GEN-LAST:event_consultasjTableMouseClicked

  private void aceptarEstudiojButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_aceptarEstudiojButtonActionPerformed
    nuevoEstudiojButton.setEnabled(true);
    setBotonesAyC(false);
    eliminarEstudiojButton.setEnabled(false);
    if (isEstudioNuevo){
      if (!estudiosjTextField.getText().equals("") ){
        try {
          HistoriaClinicaDAO hdao = HistoriaClinicaDAO.createDAO();
          if (detallejTextArea.getText().equals("")){
            int ans = JOptionPane.showConfirmDialog(this,"El estudio nuevo no tiene detalle","¿Desea crear un estudio sin detalle?",JOptionPane.YES_NO_OPTION);
            if (ans == JOptionPane.YES_OPTION) {
              EstudioDTO edto = new EstudioDTO(hdao.LastIDEstudio()+1, tdtos.get(consultasjTable.getSelectedRow()).getId(), estudiosjTextField.getText(), detallejTextArea.getText());
              hdao.SaveEstudio(edto);
              DefaultListModel listModel = (DefaultListModel) estudiosjList.getModel();
              listModel.addElement(estudiosjTextField.getText());
              estudiosjList.setModel(listModel);
              llenarEstudios(tdtos.get(consultasjTable.getSelectedRow()).getId());
            }
          }else{
            EstudioDTO edto = new EstudioDTO(hdao.LastIDEstudio()+1, tdtos.get(consultasjTable.getSelectedRow()).getId(), estudiosjTextField.getText(), detallejTextArea.getText());
            hdao.SaveEstudio(edto);
            DefaultListModel listModel = (DefaultListModel) estudiosjList.getModel();
            listModel.addElement(estudiosjTextField.getText());
            estudiosjList.setModel(listModel);
            llenarEstudios(tdtos.get(consultasjTable.getSelectedRow()).getId());
          }
          
        } catch (Exception e) {
              System.out.println("error al salvar");
        }
      }
    }
    else{
      if (!estudiosjTextField.getText().equals("") && estudioAUX != null){
        DefaultListModel listModel = (DefaultListModel) estudiosjList.getModel();
        EstudioDTO edto = new EstudioDTO(estudioAUX.getId(), estudioAUX.getId_turno(), estudiosjTextField.getText(), detallejTextArea.getText());
        try {
          estudioAUX = new Estudio(edto);
          int indice = estudiosjList.getSelectedIndex();
          HistoriaClinicaDAO hdao = HistoriaClinicaDAO.createDAO();
          hdao.EditEstudio(edto);
          listModel.setElementAt(estudioAUX, indice);
          estudios.set(indice, estudioAUX);
          detallejTextArea.setText("");
          estudiosjList.setModel(listModel);
          } catch (Exception e) {
            System.out.println("Error al editar");
          }
    }
    estudioAUX = null;    
    }
    refreshTablaConsultas();
    limpiarTextos();
  }//GEN-LAST:event_aceptarEstudiojButtonActionPerformed

  private void estudiosjListMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_estudiosjListMouseClicked
    if (!estudiosjList.isSelectionEmpty()){
      Estudio est = estudios.get(estudiosjList.getSelectedIndex());
      detallejTextArea.setText(est.getDetalle());
      estudiosjTextField.setText(est.getNombre());
      if (isEnFecha){
        eliminarEstudiojButton.setEnabled(true);
        modificarEstudiojButton.setEnabled(true);
        estudioAUX = est;
      }else{
        eliminarEstudiojButton.setEnabled(false);
        modificarEstudiojButton.setEnabled(false);
      }
    }
  }//GEN-LAST:event_estudiosjListMouseClicked

  private void guardarDiagnosticojButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_guardarDiagnosticojButtonActionPerformed
    if (!diagnosticojTextArea.getText().equals("") ){
      String detalle = diagnosticojTextArea.getText();
      try {
        HistoriaClinicaDAO hdao = HistoriaClinicaDAO.createDAO();
        hdao.AddDiagnostico(tdtos.get(consultasjTable.getSelectedRow()).getId(), detalle);
        guardarDiagnosticojButton.setEnabled(false);
        editarDiagnosticojButton.setEnabled(true);
        diagBack = detalle;
        JOptionPane.showMessageDialog(this, "Diagnostico guardado correctamente", "Exito!", JOptionPane.INFORMATION_MESSAGE);
        refreshTablaConsultas();
      } catch (Exception e) {
      }
    } else{
    JOptionPane.showMessageDialog(this, "No ha ingresado ningun diagnóstico", "Atención!", JOptionPane.WARNING_MESSAGE);
    }
  }//GEN-LAST:event_guardarDiagnosticojButtonActionPerformed

  private void editarDiagnosticojButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_editarDiagnosticojButtonActionPerformed
    if (!diagnosticojTextArea.getText().equals("") ){
      String detalle = diagnosticojTextArea.getText();
      try {
        HistoriaClinicaDAO hdao = HistoriaClinicaDAO.createDAO();
        hdao.EditDiagnostico(tdtos.get(consultasjTable.getSelectedRow()).getId(), detalle);
        diagBack = detalle;
        JOptionPane.showMessageDialog(this, "Diagnostico editado correctamente", "Exito!", JOptionPane.INFORMATION_MESSAGE);
        refreshTablaConsultas();
      } catch (Exception e) {
      }
    } else{
      try {
        JOptionPane.showMessageDialog(this, "No es posible editar un diagnóstico como vacio", "Atención!", JOptionPane.WARNING_MESSAGE);
        HistoriaClinicaDAO hdao = HistoriaClinicaDAO.createDAO();
        diagnosticojTextArea.setText(diagBack);
      } catch (Exception e) {
      }
    }
  }//GEN-LAST:event_editarDiagnosticojButtonActionPerformed

  private void cancelarEstudiojButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelarEstudiojButtonActionPerformed
    limpiarTextos();
    nuevoEstudiojButton.setEnabled(true);
    setBotonesAyC(false);
    
  }//GEN-LAST:event_cancelarEstudiojButtonActionPerformed

  private void eliminarEstudiojButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_eliminarEstudiojButtonActionPerformed
    DefaultListModel listModel = (DefaultListModel) estudiosjList.getModel();
    Estudio est = estudios.get(estudiosjList.getSelectedIndex());
    try {
      int indice = estudiosjList.getSelectedIndex();
      HistoriaClinicaDAO hdao = HistoriaClinicaDAO.createDAO();
      hdao.DelEstudio(est.getId());
      listModel.removeElementAt(indice);
      limpiarTextos();
      estudiosjList.setModel(listModel);
      estudios.remove(indice);
      } catch (Exception e) { 
      }
    eliminarEstudiojButton.setEnabled(false);
    modificarEstudiojButton.setEnabled(false);
    nuevoEstudiojButton.setEnabled(true);
    setBotonesAyC(false);
    refreshTablaConsultas();
  }//GEN-LAST:event_eliminarEstudiojButtonActionPerformed

  private void modificarEstudiojButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_modificarEstudiojButtonActionPerformed
    setBotonesAyC(true);
    setBotonesNyM(false);
    eliminarEstudiojButton.setEnabled(false);
    isEstudioNuevo = false;
  }//GEN-LAST:event_modificarEstudiojButtonActionPerformed

  private void nuevoEstudiojButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_nuevoEstudiojButtonActionPerformed
    setBotonesAyC(true);
    setBotonesNyM(false);
    estudiosjTextField.setText("Ingrese el estudio");
    detallejTextArea.setText("");
    isEstudioNuevo = true;
    
  }//GEN-LAST:event_nuevoEstudiojButtonActionPerformed
  

  // Variables declaration - do not modify//GEN-BEGIN:variables
  private javax.swing.JButton aceptarEstudiojButton;
  private javax.swing.JButton agregarAlergiajButton;
  private javax.swing.JButton agregarPatologiajButton;
  private javax.swing.JList alergiasjList;
  private javax.swing.JPanel alergiasjPanel;
  private javax.swing.JScrollPane alergiasjScrollPane;
  private javax.swing.JTextField alergiasjTextField;
  private javax.swing.JButton cancelarEstudiojButton;
  private javax.swing.JScrollPane consultasjScrollPane;
  private javax.swing.JTable consultasjTable;
  private javax.swing.JScrollPane detallejScrollPane;
  private javax.swing.JTextArea detallejTextArea;
  private javax.swing.JPanel diagnosticojPanel;
  private javax.swing.JTextArea diagnosticojTextArea;
  private javax.swing.JLabel dnijLabel;
  private javax.swing.JLabel dnijLabel1;
  private javax.swing.JButton editarDiagnosticojButton;
  private javax.swing.JButton eliminarAlergiajButton;
  private javax.swing.JButton eliminarEstudiojButton;
  private javax.swing.JButton eliminarPatologiajButton;
  private javax.swing.JList estudiosjList;
  private javax.swing.JTextField estudiosjTextField;
  private javax.swing.JButton guardarDiagnosticojButton;
  private javax.swing.JPanel hidejPanel;
  private javax.swing.JLabel jLabel1;
  private javax.swing.JScrollPane jScrollPane1;
  private javax.swing.JScrollPane jScrollPane8;
  private javax.swing.JTabbedPane jTabbedPane1;
  private javax.swing.JButton modificarEstudiojButton;
  private javax.swing.JLabel nafiliadojLabel;
  private javax.swing.JLabel nafiliadojLabel1;
  private javax.swing.JButton nuevoEstudiojButton;
  private javax.swing.JLabel obraSocialjLabel;
  private javax.swing.JLabel obraSocialjLabel1;
  private javax.swing.JLabel pacientejLabel;
  private javax.swing.JLabel pacientejLabel1;
  private javax.swing.JList patologiasjList;
  private javax.swing.JPanel patologiasjPanel;
  private javax.swing.JScrollPane patologiasjScrollPane;
  private javax.swing.JTextField patologiasjTextField;
  private javax.swing.JLabel planjLabel;
  private javax.swing.JLabel planjLabel1;
  private javax.swing.JPanel resultadosEstudiosjPanel;
  private javax.swing.JButton seleccionarPacientejButton;
  private javax.swing.JLabel turnoSelecjLabel;
  // End of variables declaration//GEN-END:variables
}
